package com.jamlu.framework.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.DrawableRes;
import android.view.View;
import android.widget.ImageView;

import com.jamlu.framework.GlideApp;
import com.jamlu.framework.R;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;


/**
 * Author ljb
 * Created at 2016/11/8.
 * Description
 * 图片工具类
 */

public class ImageLoaderUtils {

    private static int defaultImgRes = R.drawable.icon_default;

    /**
     * 显示图片
     *
     * @param imgView
     * @param url
     */
    public static void loadImage(Context context, ImageView imgView, String url) {
        GlideApp
                .with(context)
                .load(url)
                .centerCrop()
                .error(defaultImgRes)
                .transition(withCrossFade())
                .into(imgView);

    }

    /**
     * 显示图片
     *
     * @param imgView
     * @param url
     */
    public static void loadImage(Context context, ImageView imgView, String url, int defaultImgRes) {
        GlideApp
                .with(context)
                .load(url)
                .centerCrop()
                .error(defaultImgRes)
                .transition(withCrossFade())
                .into(imgView);

    }

    /**
     * 显示图片
     *
     * @param imgView
     * @param url
     */
    public static void loadImage(Context context, ImageView imgView, String url, int w, int h) {
        GlideApp
                .with(context)
                .load(url)
                .centerCrop()
                .error(defaultImgRes)
                .transition(withCrossFade())
                .override(w, h)
                .into(imgView);

    }

    /**
     * 显示图片
     *
     * @param imgView
     * @param res
     */
    public static void loadImage(Context context, ImageView imgView, @DrawableRes int res) {
        GlideApp
                .with(context)
                .load(res)
                .centerCrop()
                .error(defaultImgRes)
                .transition(withCrossFade())
                .into(imgView);

    }

    /**
     * 显示圆形图片
     * @param imgView
     * @param bitmap
     */
    public static void loadCircleImage(ImageView imgView,Bitmap bitmap){
        GlideApp
                .with(imgView)
                .load(bitmap)
                .circleCrop()
                .transition(withCrossFade())
                .into(imgView);
    }

    /**
     * 显示圆形图片
     *
     * @param imgView
     * @param url
     */
    public static void loadCircleImage(Context context, ImageView imgView, String url) {
        GlideApp
                .with(context)
                .load(url)
                .circleCrop()
                .transition(withCrossFade())
                .into(imgView);
    }

    /**
     * 显示圆形图片
     *
     * @param imgView
     * @param res
     */
    public static void loadCircleImage(Context context, ImageView imgView, int res) {
        GlideApp
                .with(context)
                .load(res)
                .circleCrop()
                .transition(withCrossFade())
                .into(imgView);
    }

//    /**
//     * 显示裁剪图片
//     *
//     * @param imgView
//     * @param url
//     */
//    public static void loadCutImage(final Context context, final ImageView imgView, final String url) {
//        DrawableRequestBuilder<Integer> thumbnail = GlideApp.with(context)
//                .load(defaultImgRes);
//        GlideApp
//                .with(context)
//                .load(url)
//                .diskCacheStrategy(DiskCacheStrategy.RESULT)
//                .skipMemoryCache(false)
//                .centerCrop()
//                .crossFade()
//                .thumbnail(thumbnail)
//                .into(new SimpleTarget<GlideDrawable>() {
//                    @Override
//                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
//                        int width = ScreenUtils.getScreenWidth()/2;
//                        int minimumWidth = resource.getMinimumWidth();
//                        int height = (int) (width/(minimumWidth+0f)*resource.getMinimumHeight());
//                        ViewGroup.LayoutParams layoutParams = imgView.getLayoutParams();
//                        layoutParams.height = height;
//                        layoutParams.width = width;
//                        imgView.setImageDrawable(resource);
//                    }
//                });
//    }
//    /**
//     * 显示裁剪图片
//     *
//     * @param imgView
//     * @param path
//     */
//    public static void loadCutImage(final Context context, final ImageView imgView, final String path) {
//        Observable.create(new Observable.OnSubscribe<Bitmap>() {
//            @Override
//            public void call(Subscriber<? super Bitmap> subscriber) {
//                subscriber.onNext(decodeBitmapByUrl(path));
//                subscriber.onCompleted();
//            }
//        }).subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Action1<Bitmap>() {
//                    @Override
//                    public void call(Bitmap bitmap) {
//                        DrawableRequestBuilder<Integer> thumbnail = GlideApp.with(context)
//                                .load(defaultImgRes)
//                                .bitmapTransform(new CustomShapeTransformation(context, path, bitmap));
//                        GlideApp
//                                .with(context)
//                                .load(path)
//                                .diskCacheStrategy(DiskCacheStrategy.RESULT)
//                                .skipMemoryCache(false)
//                                .centerCrop()
//                                .crossFade()
//                                .transform(new CustomShapeTransformation(context, path, bitmap))
//                                .thumbnail(thumbnail)
//                                .into(new SimpleTarget<GlideDrawable>() {
//                                    @Override
//                                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
//                                        int minimumWidth = resource.getMinimumWidth();
//                                        int minimumHeight = resource.getMinimumHeight();
//                                        ViewGroup.LayoutParams layoutParams = imgView.getLayoutParams();
//                                        layoutParams.height = minimumHeight;
//                                        layoutParams.width = minimumWidth;
//                                        imgView.setImageDrawable(resource);
//                                    }
//                                });
//                    }
//                });
//    }

    /**
     * url转bitmap
     *
     * @param path
     */
    public static Bitmap decodeBitmapByUrl(String path) {
        try {
            if (path.startsWith("http")) {
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                InputStream is = conn.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;

                while ((len = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                byte[] result = baos.toByteArray();
                return BitmapFactory.decodeByteArray(result, 0, result.length);
            } else {
                return BitmapFactory.decodeFile(path);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 设置默认图片
     *
     * @param imgRes
     */
    public static void setDefaultImgRes(@DrawableRes int imgRes) {
        defaultImgRes = imgRes;
    }

    /**
     * 取消请求
     */
    public static void clear(View view) {
        GlideApp.with(view).clear(view);
    }
}
